package com.quartz1.controller;

import com.quartz1.common.CommonResult;
import com.quartz1.job.CronProcessJob;
import com.quartz1.model.JobAndTrigger;
import com.quartz1.model.JobInfo;
import com.quartz1.service.ScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定时任务调度相关接口
 * Created by QT on 2022/2/9.
 */
@Api(tags = "ScheduleController", description = "定时任务调度相关接口")
@RestController
@RequestMapping("/schedule")
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("新增定时任务")
    @PostMapping("/scheduleJob")
    public CommonResult scheduleJob(JobInfo jobInfo) throws Exception{
        Boolean success = scheduleService.scheduleJob(jobInfo);
        return CommonResult.success(success);
    }

    @ApiOperation("新增定时任务1")
    @PostMapping("/scheduleJob1")
    public CommonResult scheduleJob1(@RequestParam String cron, @RequestParam String data) {
        String jobName = scheduleService.scheduleJob(CronProcessJob.class, cron, data);
        return CommonResult.success(jobName);
    }

    /**
     *
     * @param jobName  查询条件，查不到会返回500
     * @param jobGroup 查询条件，查不到会返回500
     * @param cronExpression 需要更新的Cron表达式
     * @return
     * @throws Exception
     */
    @ApiOperation("更新定时任务")
    @PostMapping(value = "/reschedulejob")
    public CommonResult rescheduleJob(@RequestParam(value = "jobName") String jobName,
                              @RequestParam(value = "jobGroup") String jobGroup,
                              @RequestParam(value = "cronExpression") String cronExpression) throws Exception {
        Boolean success = scheduleService.reScheduleJob(jobName, jobGroup, cronExpression);
        return CommonResult.success(success);
    }

    @ApiOperation("暂停定时任务")
    @PostMapping(value = "/pausejob")
    public CommonResult pausejob(@RequestParam(value = "jobName") String jobName, @RequestParam(value = "jobGroup") String jobGroup) throws Exception {
        Boolean success = scheduleService.pausejob(jobName, jobGroup);
        return CommonResult.success(success);
    }

    @ApiOperation("恢复定时任务")
    @PostMapping(value = "/resumejob")
    public CommonResult resumejob(@RequestParam(value = "jobName") String jobName, @RequestParam(value = "jobGroup") String jobGroup) throws Exception {
        Boolean success = scheduleService.resumejob(jobName, jobGroup);
        return CommonResult.success(success);
    }

    @ApiOperation("取消定时任务")
    @PostMapping("/cancelScheduleJob")
    public CommonResult cancelScheduleJob(@RequestParam String jobName) {
        Boolean success = scheduleService.cancelScheduleJob(jobName);
        return CommonResult.success(success);
    }

    @ApiOperation("查询定时任务")
    @GetMapping(value = "/queryScheduleJob")
    public CommonResult queryScheduleJob(@ApiParam(name = "pageNow", value = "页码", required = true, defaultValue = "1")
                                        @RequestParam int pageNow,
                                        @ApiParam(name = "pageSize", value = "每页数量", required = true, defaultValue = "3")
                                        @RequestParam int pageSize) {
        List<JobAndTrigger> jobs = scheduleService.getJobAndTriggerDetails(pageNow,pageSize);
        return CommonResult.success(jobs);
    }
}
